package com.cskaoyan.controller.topic.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Comment;

import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.coupon.wx.ListData;
import com.cskaoyan.bean.topic.TopicDetailVO;
import com.cskaoyan.bean.topic.TopicDetailWithCommentsVO;
import com.cskaoyan.service.comment.wx.CommentService;
import com.cskaoyan.service.topic.wx.TopicService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 微信小程序端
 */
@RestController
@RequestMapping("/wx/topic")
public class TopicController2 {

    @Autowired
    TopicService2 topicService2;
    @Autowired
    CommentService commentService;

    /**
     *
     * @param valueId
     * @param type
     * @param showType :????
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(String valueId,String type,String showType,Integer page,Integer size){
        if (valueId == null && type == null && showType == null && page != null && size != null){
            //点击专题列表，显示所有专题
            ListData<Topic> listData = topicService2.list(page,size);
            return BaseRespVo.ok(listData);
        }else if (valueId != null && type != null && showType != null && page != null && size != null){
            //点击某个专题，显示专题信息和评论
            TopicDetailWithCommentsVO topicDetailWithCommentsVO = topicService2.list( valueId, type, showType, page, size);
            return BaseRespVo.ok(topicDetailWithCommentsVO);
        }
        return BaseRespVo.fail("拉取专题失败，请重试");
    }

    /**
     * 微信小程序端点击首页的专题后，会提交三个请求：detail、related、list
     * @param id
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo detail(String id){
        Topic detail = topicService2.detail(id);
        if (detail != null){
            TopicDetailVO topicDetailVO = new TopicDetailVO();
            topicDetailVO.setTopic(detail);
            topicDetailVO.setGoods(new LinkedList<>());
            return BaseRespVo.ok(topicDetailVO,"成功");
        }else {
            return BaseRespVo.fail("failed,try again ");
        }
    }

    /**
     * 推荐专题，四条
     * 另：根据专题热度推荐
     * @param id
     * @return
     */
    @RequestMapping("related")
    public BaseRespVo related(String id){
        List<Topic> related = topicService2.related(id);
        if (related != null){
            return BaseRespVo.ok(related,"成功");
        }else {
            return BaseRespVo.fail("failed,try again ");
        }
    }

    /**
     * 专题下的评论使用了comment表
     */
    @PostMapping("post")
    @Transactional
    public BaseRespVo post(@RequestBody Comment comment){
        boolean post = commentService.post(comment);
        if (post){
            int lastInsertId = commentService.getLastInsertId();
            return BaseRespVo.ok(commentService.selectByPrimaryKey(lastInsertId),"成功");
        }else {
            return BaseRespVo.fail("failed,try again ");
        }
    }
}
